import config from '@/config/index.js';
import guid from '@/components/uview-ui/libs/function/guid.js'
import dayjs from '@/js_sdk/dayjs/dayjs.min.js'
import store from '@/store/index.js'

const wlog = {
	// 设备唯一标识，系统自动生成
	deviceId: null,

	isLogPrintConsole: false,
	
	openLog() {
		uni.setStorageSync('isLogPrintConsole', 'yes')
		wlog.isLogPrintConsole = true
	},
	
	closeLog() {
		uni.removeStorageSync('isLogPrintConsole')
		wlog.isLogPrintConsole = false
	},

	// 初始化日志系统
	init() {
		
		if (__wxConfig.envVersion == 'release') {
			wlog.isLogPrintConsole = false
		} else {
			wlog.isLogPrintConsole = true
		}

		if (!wlog.deviceId) {
			// 如果还没有读取设备ID，则先进行读取
			let _deviceId = uni.getStorageSync('lbb-device-id')
			if (!_deviceId) {
				// 如果本地缓存中没有设备ID，则自动生成一个
				_deviceId = guid(10, false, 10)
				uni.setStorageSync('lbb-device-id', _deviceId)
			}
			wlog.deviceId = _deviceId
		}

		console.log = (function(oriLogFunc) {
			return function(...args) {
				if (wlog.isLogPrintConsole) {
					oriLogFunc.call(console,
						`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...args);
				}
				// #ifdef MP-WEIXIN
				if (config.log_wx_realtime) {
					var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
					if (log) {
						log.info(`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...args)
					}
				}
				// #endif
			}
		})(console.log);

		console.debug = (function(oriLogFunc) {
			return function(...args) {
				if (wlog.isLogPrintConsole) {
					oriLogFunc.call(console,
						`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...args);
				}
				// #ifdef MP-WEIXIN
				if (config.log_wx_realtime) {
					var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
					if (log) {
						log.debug(`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...
							args)
					}
				}
				// #endif
			}
		})(console.debug);

		console.error = (function(oriLogFunc) {
			return function(...args) {
				if (wlog.isLogPrintConsole) {
					oriLogFunc.call(console,
						`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...args);
				}
				// #ifdef MP-WEIXIN
				if (config.log_wx_realtime) {
					var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
					if (log) {
						log.error(`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...
							args)
					}
				}
				// #endif
			}
		})(console.error);

		console.warn = (function(oriLogFunc) {
			return function(...args) {
				if (wlog.isLogPrintConsole) {
					oriLogFunc.call(console,
						`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...args);
				}
				// #ifdef MP-WEIXIN
				if (config.log_wx_realtime) {
					var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
					if (log) {
						log.warn(`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...args)
					}
				}
				// #endif
			}
		})(console.warn);

		console.info = (function(oriLogFunc) {
			return function(...args) {
				if (wlog.isLogPrintConsole) {
					oriLogFunc.call(console,
						`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...args);
				}
				// #ifdef MP-WEIXIN
				if (config.log_wx_realtime) {
					var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
					if (log) {
						log.info(`【${wlog.dateTimeStr()}】【${wlog.deviceId}】【${wlog.userId()}】`, ...args)
					}
				}
				// #endif
			}
		})(console.info);
	},

	dateTimeStr() {
		return dayjs().format('YYYY-MM-DD HH:mm:ss.SSS');
	},

	userId() {
		if (store.getters.userInfo) {
			return `${store.getters.userInfo.uid}-${store.getters.userInfo.uphone}`
		}
		return '未登录'
	},

}

wlog.init();

export default wlog
